import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default {
    state: {
        menuList: [],
        permList: [],
        hasRoute: false,

        //默认选中的标签的name 也就是他的路径
        editableTabsValue: 'index',
        //所有的标签 标签对象有title和name 其中title为路径的标题 name为路径
        editableTabs: []
    },
    mutations: {
        CHANGE_ROUTER_STATUS(state,hasRouter){
            state.hasRoute = hasRouter
        }
    },
    actions:{
        changeRouterStatus({commit},hasRouter){
            commit("CHANGE_ROUTER_STATUS",hasRouter)
            sessionStorage.setItem("hasRouter",hasRouter)
        },
        //设置用户的菜单
        setMenuList({state}, menus) {
            state.menuList = menus
        },
        //用户的权限
        setPermList({state}, authoritys) {
            state.permList = authoritys
        },
        //添加标签
        addTab({state},tab) {
            //在已经有的所有的标签中寻找加入的标签是否存在
            let index = state.editableTabs.findIndex(e=>e.name==tab.name)
            //如果不存在
            if(index===-1){
                //把他加进去标签数组
                state.editableTabs.push({
                    title: tab.title,
                    name: tab.name,
                });
            }
            //选中加入的标签
            state.editableTabsValue = tab.name;
        },
        resetState({state}){
               state.menuList= [],
                state.permList=[],
                state.hasRoute= false,

                state.editableTabsValue='index',
                state.editableTabs=[{
                title: '首页',
                name: 'index',
            }]
        }
    },
    getters:{
        getRouterStatus(state){
            return state.hasRoute
        },
        getMenuList(state){
            return state.menuList
        }
    }
}
